package com.xframe.domain;

import com.xframe.domain.role.IUserCredentials;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.xframe.xdal.core.annotation.AutoFill;
import com.xframe.xdal.core.annotation.XColumn;
import com.xframe.xdal.core.annotation.XQueryCondition;
import com.xframe.xdal.core.constant.OperateMark;
import com.xframe.xdal.core.domain.SysBaseEntity;
import com.xframe.xdal.core.util.StringUtil;
import lombok.Data;

import java.util.Date;

/**
 * 系统对象的基类
 *
 * @author xufeng
 * @version 1.0
 * @CreateTime 2022-09-19
 */
@Data
public class BaseModel extends SysBaseEntity{
    /**
     * 创建人
     */
    @AutoFill(defVal = "SYS")
    @XColumn(readonly = true,length = 64,desc = "创建人")
    private String createBy;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @XColumn(readonly = true,useDbObjectFlag = true,dbExpression = "SYSDATE",desc = "创建时间")
    private Date createTime = new Date();

    /**
     * 修改人
     */
    @AutoFill(defVal = "SYS",operateMark = OperateMark.Create | OperateMark.Update)
    @XColumn(length = 64,desc = "修改人")
    private String updateBy;

    /**
     * 修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @XColumn(useDbObjectFlag = true, dbExpression = "SYSDATE",desc = "修改时间")
    private Date updateTime = new Date();

    /**
     * 版本号
     */
    @AutoFill(defVal = "0")
    @XColumn(versionFlag = true,desc = "版本号")
    private Integer version;

    /**
     * 是否有效(删除标记)
     * 0 = 有效 1 = 无效
     */
    @XQueryCondition
    @AutoFill(defVal = "0")
    @XColumn(deleteFlag = true,desc = "是否有效")
    private Integer delFlag;

    public final static String CREATEBY_NAME = "createBy";

    public final static String CREATETIME_NAME = "createTime";

    public final static String UPDATEBY_NAME = "updateBy";

    public final static String UPDATETIME_NAME = "updateTime";

    public final static String DELFLAG_NAME = "delFlag";

    public final static String VERSION_NAME = "version";

    public final static String STATUS_NAME = "status";

    public final static Integer DEL_FALG_Y = 1;
    public final static Integer DEL_FALG_N = 0;



    public void setBaseAttrVal(IUserCredentials userCredentials){
        if(StringUtil.isEmpty(this.getCreateBy())){
            this.setCreateBy(userCredentials.getName());
        }
        this.setUpdateBy(userCredentials.getName());
    }

    /**
     * 设置创建人和最后修改人
     * @param userName 创建人和最后修改人
     */
    public void setBaseAttrVal(String userName){
        if(StringUtil.isEmpty(this.getCreateBy())){
            this.setCreateBy(userName);
        }
        this.setUpdateBy(userName);
    }


    public void setBaseAttrValByUpdate(IUserCredentials userCredentials){
        this.setUpdateBy(userCredentials.getName());
//        this.setUpdateDept(userCredentials.getDeptId());
//        this.setUpdateIp(userCredentials.getClientIp());
    }













}
